package com.downloading.bezoyaapp;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.downloading.bezoyaapp.gcm.ServerUtilities;
import com.downloading.bezoyaapp.getData.WebServiceAccess;
import com.downloading.bezoyaapp.support.utils.Constraints;
import com.google.android.gcm.GCMRegistrar;

public class PushPrueba extends Activity {

	Button registrarButton;
	Button desRegistrarButton;
	AsyncTask<Void, Void, Void> mRegisterTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_push_prueba);

		registrarButton = (Button) findViewById(R.id.botonRegistrar);
		desRegistrarButton = (Button) findViewById(R.id.botonDesRegistrar);

		registrarButton.setOnClickListener(new RegistrarClick(this));
		desRegistrarButton.setOnClickListener(new DesRegistrarClick(this));

		registerReceiver(mHandleMessageReceiver, new IntentFilter("prueba"));

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_push_prueba, menu);
		return true;
	}

	@Override
	protected void onDestroy() {
		if (mRegisterTask != null) {
			mRegisterTask.cancel(true);
		}
		unregisterReceiver(mHandleMessageReceiver);
		GCMRegistrar.onDestroy(this);
		super.onDestroy();
	}

	private class RegistrarClick implements OnClickListener {

		Context context;

		public RegistrarClick(Context pushPrueba) {

			context = pushPrueba;
		}

		@Override
		public void onClick(View v) {

			Log.i("BezoyaApp", "Empezamos GCM");
			GCMRegistrar.checkDevice(context);
			Log.i("BezoyaApp", "Chequeamos device");
			GCMRegistrar.checkManifest(context);
			Log.i("BezoyaApp", "Chequeamos manifest");

			final String regId = GCMRegistrar.getRegistrationId(context);

			// Registramos la App en el servidor.
			if (regId.equals("")) {
				// Automatically registers application on startup.
				Log.i("BezoyaApp", "Intentamos Registrar");
				GCMRegistrar.register(context, Constraints.SENDER_ID);
				Log.i("BezoyaApp", "Registrado");

			} else {
				// Device is already registered on GCM, check server.
				if (GCMRegistrar.isRegisteredOnServer(context)) {
					// Skips registration.
					Log.i("BezoyaApp", "Ya Registrado");

					Toast.makeText(context, "Ya Registrado", Toast.LENGTH_LONG)
							.show();
				} else {
					// Try to register again, but not in the UI thread.
					// It's also necessary to cancel the thread onDestroy(),
					// hence the use of AsyncTask instead of a raw thread.
					Log.i("BezoyaApp", "ReRegistramos");
					mRegisterTask = new AsyncTask<Void, Void, Void>() {

						@Override
						protected Void doInBackground(Void... params) {
							ServerUtilities.register(context, regId);
							return null;
						}

						@Override
						protected void onPostExecute(Void result) {
							mRegisterTask = null;
						}

					};
					mRegisterTask.execute(null, null, null);
				}
			}

		}

	}

	private class DesRegistrarClick implements OnClickListener {

		Context context;

		public DesRegistrarClick(Context pushPrueba) {

			context = pushPrueba;
		}

		@Override
		public void onClick(View v) {

			/*
			 * Log.i("BezoyaApp", "Empezamos GCM");
			 * GCMRegistrar.checkDevice(context); Log.i("BezoyaApp",
			 * "Chequeamos device"); GCMRegistrar.checkManifest(context);
			 * Log.i("BezoyaApp", "Chequeamos manifest");
			 * 
			 * final String regId = GCMRegistrar.getRegistrationId(context);
			 * 
			 * if (!regId.equals("")) { // Automatically registers application
			 * on startup. Log.i("BezoyaApp", "Intentamos Registrar");
			 * ServerUtilities.unregister(context, regId); Log.i("BezoyaApp",
			 * "Registrado");
			 */

			WebServiceAccess access = new WebServiceAccess();
			access.prueba();

		}

	}

	private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			Toast.makeText(context, "Sergio!!!", Toast.LENGTH_LONG).show();
		}
	};

}
